package com.awanp3.nimbus.tool.asset.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.awanp3.nimbus.common.idempotent.annotation.RepeatSubmit;
import com.awanp3.nimbus.common.log.annotation.Log;
import com.awanp3.nimbus.common.web.core.BaseController;
import com.awanp3.nimbus.common.mybatis.core.page.PageQuery;
import com.awanp3.nimbus.common.core.domain.R;
import com.awanp3.nimbus.common.core.validate.AddGroup;
import com.awanp3.nimbus.common.core.validate.EditGroup;
import com.awanp3.nimbus.common.log.enums.BusinessType;
import com.awanp3.nimbus.common.excel.utils.ExcelUtil;
import com.awanp3.nimbus.tool.asset.domain.vo.HostVo;
import com.awanp3.nimbus.tool.asset.domain.bo.HostBo;
import com.awanp3.nimbus.tool.asset.service.IHostService;
import com.awanp3.nimbus.common.mybatis.core.page.TableDataInfo;

/**
 * 主机
 * 前端访问路由地址为:/tool/host
 *
 * @author 20014170
 * @date 2025-08-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/host")
public class HostController extends BaseController {

    private final IHostService hostService;

    /**
     * 查询主机列表
     */
    @SaCheckPermission("tool:host:list")
    @GetMapping("/list")
    public TableDataInfo<HostVo> list(HostBo bo, PageQuery pageQuery) {
        return hostService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出主机列表
     */
    @SaCheckPermission("tool:host:export")
    @Log(title = "主机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HostBo bo, HttpServletResponse response) {
        List<HostVo> list = hostService.queryList(bo);
        ExcelUtil.exportExcel(list, "主机", HostVo.class, response);
    }

    /**
     * 获取主机详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tool:host:query")
    @GetMapping("/{id}")
    public R<HostVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(hostService.queryById(id));
    }

    /**
     * 新增主机
     */
    @SaCheckPermission("tool:host:add")
    @Log(title = "主机", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HostBo bo) {
        return toAjax(hostService.insertByBo(bo));
    }

    /**
     * 修改主机
     */
    @SaCheckPermission("tool:host:edit")
    @Log(title = "主机", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HostBo bo) {
        return toAjax(hostService.updateByBo(bo));
    }

    /**
     * 删除主机
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tool:host:remove")
    @Log(title = "主机", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(hostService.deleteWithValidByIds(List.of(ids), true));
    }
}
